package com.swgk.core.customview;

import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.widget.EditText;

public class DecimalInputTextWatcher implements TextWatcher {
    /**
     * 需要设置该 DecimalInputTextWatcher 的 EditText
     */
    private EditText editText;

    /**
     * 默认  小数的位数   2 位
     */
    private static final int DEFAULT_DECIMAL_DIGITS = 2;

    /**
     * 小数的位数
     */
    private int decimalDigits;
    /**
     * 整数的位数
     */
    private int integerDigits;
    /**
     * 第一位是否能输入0
     */
    private boolean isFirstInputZero;


    public DecimalInputTextWatcher(EditText editText) {
        this.editText = editText;
        this.decimalDigits = DEFAULT_DECIMAL_DIGITS;
    }

    /**
     * @param editText      editText
     * @param decimalDigits 小数的位数
     */
    public DecimalInputTextWatcher(EditText editText, int decimalDigits) {
        this.editText = editText;
        if (decimalDigits <= 0) {
            throw new RuntimeException("decimalDigits must > 0");
        }
        this.decimalDigits = decimalDigits;
    }

    /**
     * @param editText      editText
     * @param integerDigits 整数的位数
     * @param decimalDigits 小数的位数
     */
    public DecimalInputTextWatcher(EditText editText, int integerDigits, int decimalDigits, boolean isInputFist) {
        this.editText = editText;
        if (integerDigits <= 0) {
            throw new RuntimeException("integerDigits must > 0");
        }
        if (decimalDigits <= 0) {
            throw new RuntimeException("decimalDigits must > 0");
        }
        this.integerDigits = integerDigits;
        this.decimalDigits = decimalDigits;
        this.isFirstInputZero = isInputFist;
    }

    /**
     * @param editText      editText
     * @param integerDigits 整数的位数
     * @param decimalDigits 小数的位数
     */
    public DecimalInputTextWatcher(EditText editText, int integerDigits, int decimalDigits) {
        this.editText = editText;
        if (integerDigits <= 0) {
            throw new RuntimeException("integerDigits must > 0");
        }
        if (decimalDigits <= 0) {
            throw new RuntimeException("decimalDigits must > 0");
        }
        this.integerDigits = integerDigits;
        this.decimalDigits = decimalDigits;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String s = editable.toString();
        editText.removeTextChangedListener(this);

        if (s.contains(".")) {
            if (integerDigits > 0) {
                editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(integerDigits + decimalDigits + 1)});
            }
            if (s.length() - 1 - s.indexOf(".") > decimalDigits) {
                s = s.substring(0,
                        s.indexOf(".") + decimalDigits + 1);
                editable.replace(0, editable.length(), s.trim());
            }
        } else {
            if (integerDigits > 0) {
                editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(integerDigits + 1)});
                if (s.length() > integerDigits) {
                    s = s.substring(0, integerDigits);
                    editable.replace(0, editable.length(), s.trim());
                }
            }

        }
        if (".".equals(s.trim())) {
            s = "0" + s;
            editable.replace(0, editable.length(), s.trim());
        }

        if (isFirstInputZero) {
            if ((s.startsWith("0") || s.startsWith("."))) {
                editable.replace(0, editable.length(), "");
            }
        } else {
            //多个0开头，只输入一个0
            if (s.startsWith("0") && s.trim().length() > 1) {
                if (s.charAt(1) != '.') {
                    editable.replace(0, editable.length(), "0");
                }
            }
        }
        editText.addTextChangedListener(this);

    }

}
